
#include "cfg_main.h"

static INT32 tabs = 0;

static INT32 xml_put(INT8 *str, INT8 *p)
{
    return sprintf(p, "%s", str);
}

INT32 xml_put_head(INT8 *head, INT8 *p)
{
    INT8 *sp = p;

    p += xml_put("<?", p);
    p += xml_put(head, p);
    p += xml_put("?>\n", p);

    return p - sp;
}

INT32 xml_out_tabs(INT8 *p)
{
    INT32 i;
    INT8 *sp = p;

    for (i = 0; i < tabs; i++)
    {
        p += xml_put("\t", p);
    }

    return p - sp;
}

INT32 xml_tag_start(INT8 *tag, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);
    
    p += xml_put("<", p);
    p += xml_put(tag, p);
    p += xml_put(">\n", p);
    
    tabs = tabs + 1;

    return p - sp;
}
INT32 xml_tag_start_para
    (INT8 *tag, INT8 *para1_name, INT8 *para1, INT8 *para2_name, INT8 *para2, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);
    
    p += xml_put("<", p);
    p += xml_put(tag, p);    
    p += xml_put(" ", p);
    
    p += xml_put(para1_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para1, p);    
    p += xml_put("\" ", p);
    
    p += xml_put(para2_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para2, p);    
    p += xml_put("\">\n", p);
    
    tabs = tabs + 1;

    return p - sp;
}

INT32 xml_tag_start_para_num
    (INT8 *tag, INT8 *para1_name, INT32 para1, INT8 *para2_name, INT32 para2, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);
    
    p += xml_put("<", p);
    p += xml_put(tag, p);    
    p += xml_put(" ", p);
    
    p += xml_put(para1_name, p);
    p += xml_put("=", p);
    p += sprintf(p, "\"%d\"", para1);
    p += xml_put(" ", p);
    
    p += xml_put(para2_name, p);
    p += xml_put("=", p);
    p += sprintf(p, "\"%d\"", para2);
    
    p += xml_put(">\n", p);
    
    tabs = tabs + 1;

    return p - sp;
}

INT32 xml_tag_start_3para
    (INT8 *tag, 
    INT8 *para1_name, INT8 *para1, 
    INT8 *para2_name, INT8 *para2, 
    INT8 *para3_name, INT8 *para3, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);
    
    p += xml_put("<", p);
    p += xml_put(tag, p);    
    p += xml_put(" ", p);
    
    p += xml_put(para1_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para1, p);    
    p += xml_put("\" ", p);
    
    p += xml_put(para2_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para2, p);
    p += xml_put("\" ", p);
    
    p += xml_put(para3_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para3, p);    
    
    p += xml_put("\">", p);

    p += xml_put("</", p);
    p += xml_put(tag, p);
    p += xml_put(">\n", p);
    
    //tabs = tabs + 1;

    return p - sp;
}

INT32 xml_tag_end(INT8 *tag, INT8 *p)
{
    INT8 *sp = p;

    tabs = tabs - 1;    
    p += xml_out_tabs(p);
    
    p += xml_put("</", p);
    p += xml_put(tag, p);
    p += xml_put(">\n", p);

    return p - sp;
}

INT32 xml_tag_value(INT8 *tag, INT8 *value, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);
    
    p += xml_put("<", p);
    p += xml_put(tag, p);    
    p += xml_put(">", p);

    p += xml_put(value, p);
    
    p += xml_put("</", p);
    p += xml_put(tag, p);    
    p += xml_put(">\n", p);

    return p - sp;
}

INT32 xml_tag_value_num(INT8 *tag, INT32 value, INT8 *p)
{
    INT8 *sp = p;
    
    p += xml_out_tabs(p);

    p += xml_put("<", p);
    p += xml_put(tag, p);
    p += xml_put(">", p);

    p += sprintf(p, "%d", value);
    
    p += xml_put("</", p);
    p += xml_put(tag, p);    
    p += xml_put(">\n", p);

    return p - sp;
}

INT32 xml_tag_para_value
(INT8 *tag, INT8 *para1_name, INT8 *para1, INT8 *para2_name, INT8 *para2, INT8 *value, INT8 *p)
{
    INT8 *sp = p;

    p += xml_out_tabs(p);

    p += xml_put("<", p);
    p += xml_put(tag, p);    
    p += xml_put(" ", p);
    
    p += xml_put(para1_name, p);
    p += xml_put("=\"", p);
    p += xml_put(para1, p);    
    p += xml_put("\" ", p);
        
    p += xml_put(para2_name, p);
    p += xml_put("=\"", p);
    // p += xml_put(para2, p);
    p += xml_put("\">", p);

    p += xml_put(value, p);

    p += xml_put("</", p);
    p += xml_put(tag, p);
    p += xml_put(">\n", p);

    return p - sp;
}

INT32 xml_tag_para_value_num
(INT8 *tag, INT8 *para1_name, INT32 para1, INT8 *para2_name, INT32 para2, INT32 value, INT8 *p)
{
    INT8 *sp = p;
    
    p += xml_out_tabs(p);

    p += xml_put("<", p);
    p += xml_put(tag, p);
    p += xml_put(para1_name, p);
    p += xml_put("=", p);
    p += sprintf(p, "\"%d\"", para1);
    p += xml_put(" ", p);
    p += xml_put(para2_name, p);
    p += xml_put("=", p);
    // p += sprintf(p, "\"%d\"", para2);
    p += xml_put(">", p);

    p += sprintf(p, "\"%d\"", value);
    
    p += xml_put("</", p);
    p += xml_put(tag, p);    
    p += xml_put(">\n", p);

    return p - sp;
}

